package com.moon.back.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author newBing
 */
@Component("hdfsTransactionUtils")
public class TransactionUtils {

    // 注入PlatformTransactionManager实例
    private static PlatformTransactionManager transactionManager;

    @Autowired
    public void setTransactionManager(PlatformTransactionManager transactionManager) {
        TransactionUtils.transactionManager = transactionManager;
    }

    public static PlatformTransactionManager getTransactionManager(String ds) {
         return null;
    }


    public static <T> T executeWithTransaction(  Supplier<T> supplier, Integer isolationLevel, Integer propagationBehavior) {
        return executeWithTransaction(supplier, isolationLevel, propagationBehavior, null);
    }

    public static <T> T executeWithTransaction( Supplier<T> supplier, Integer isolationLevel, Integer propagationBehavior, String ds) {
        // 创建事务定义对象
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();

        // 设置事务隔离级别，如果没有传入则使用默认值
        def.setIsolationLevel(Objects.requireNonNullElse(isolationLevel, TransactionDefinition.ISOLATION_DEFAULT));

        // 设置事务传播行为，如果没有传入则使用默认值
        def.setPropagationBehavior(Objects.requireNonNullElse(propagationBehavior, TransactionDefinition.PROPAGATION_REQUIRED));


        PlatformTransactionManager tmpTransactionManager = null;
        if (ds != null) {
            // DynamicDataSourceContextHolder
//            DynamicDataSourceContextHolder.push(ds);
            tmpTransactionManager = getTransactionManager(ds);
        }

        if (tmpTransactionManager == null) {
            tmpTransactionManager = transactionManager;
        }
        // 获取事务状态对象
        TransactionStatus status = tmpTransactionManager.getTransaction(def);

        try {
            // 执行runnable的run方法
            T t = supplier.get();
            // 提交事务
            tmpTransactionManager.commit(status);

            return t;
        } catch (Throwable e) {
            // 回滚事务
            tmpTransactionManager.rollback(status);
            throw e;
        } finally {
            if (ds != null) {
//                DynamicDataSourceContextHolder.poll();
            }
        }

    }

    public static void executeWithTransaction(Runnable runnable) {
        executeWithTransaction(() -> {
            runnable.run();
            return null;
        }, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED);
    }
//
//    public static <T> T executeWithTransaction(Supplier<T> supplier) {
//        return executeWithTransaction(supplier, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED);
//    }
//
//    public static <T> T executeWithTransaction(Supplier<T> supplier, String ds) {
//        return executeWithTransaction(supplier, TransactionDefinition.ISOLATION_DEFAULT, TransactionDefinition.PROPAGATION_REQUIRED, ds);
//    }
}
